import 'package:flutter/material.dart';
import 'package:interview/components/tab_item.dart';
import 'package:interview/config/app.enum.dart';
import 'package:interview/views/home/home.dart';
import 'package:interview/views/interview/interview.dart';
import 'package:interview/views/mine/mine.dart';

class Tabbar extends StatefulWidget {
  const Tabbar({super.key});

  @override
  TabbarState createState() => TabbarState();
}

class TabbarState extends State<Tabbar> with SingleTickerProviderStateMixin {
  late TabController _tabController;
  int currentIndex = 0;
  final List<Map<String, String>> tabItems = [
    {
      "defaultIcon": "${AppEnumAssets.basePath}tabbar_home.png",
      "selectedIcon": "${AppEnumAssets.basePath}tabbar_home_fill.png",
      "label": "首页",
    },
    {
      "defaultIcon": "${AppEnumAssets.basePath}tabbar_interview.png",
      "selectedIcon": "${AppEnumAssets.basePath}tabbar_interview_fill.png",
      "label": "面经",
    },
    {
      "defaultIcon": "${AppEnumAssets.basePath}tabbar_mine.png",
      "selectedIcon": "${AppEnumAssets.basePath}tabbar_mine_fill.png",
      "label": "我的",
    },
  ];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabItems.length, vsync: this);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  void onTabTapped(int index) {
    setState(() {
      currentIndex = index;
      if (_tabController.index != index) {
        _tabController.animateTo(index);
      }
    });
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: null,
      body: TabBarView(
        controller: _tabController,
        physics: const NeverScrollableScrollPhysics(),
        children: const [
          Home(),
          Interview(),
          Mine(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        onTap: onTabTapped,
        type: BottomNavigationBarType.fixed,
        // 动态设置选中和未选中标签的样式
        selectedLabelStyle: const TextStyle(
          fontSize: 10,
          fontWeight: FontWeight.bold,
        ),
        unselectedLabelStyle: const TextStyle(
          fontSize: 10,
        ),
        selectedItemColor:  const Color(0xFF000000), // 设置选中项颜色
        unselectedItemColor: const Color(0xFF9E9E9E), // 设置未选中项颜色
        // 动态控制是否显示 label
        showSelectedLabels: true, // 是否显示选中的 label
        showUnselectedLabels: true, // 是否显示未选中的 label
        items: tabItems.map((item) {
          final index = tabItems.indexOf(item);
          return BottomNavigationBarItem(
            icon: Padding(
              padding: EdgeInsets.zero, // 清除任何默认的内边距
              child: TabBarItem(
                defaultIconPath: item["defaultIcon"]!,
                selectedIconPath: item["selectedIcon"]!,
                isSelected: currentIndex == index,
              ),
            ),
            label: item["label"]!,
          );
        }).toList(),
      ),
    );
  }
}
